var form, $, areaData;
layui.config({
	base: "../../../../js/"
}).extend({
	"address": "address"
})
layui.use(['form', 'layer', 'upload', 'laydate', "address"], function () {
	form = layui.form;
	$ = layui.jquery;
	var layer = parent.layer === undefined ? layui.layer : top.layer,
		upload = layui.upload,
		laydate = layui.laydate,
		address = layui.address;
		LoginName = localStorage.getItem("LoginName"); 
		$("#name").val(LoginName);
	//上传头像
	upload.render({
		elem: '#userFace',
		url: 'http://127.0.0.1:8000/uploadImg',
		method: "post", //此处是为了演示之用，实际使用中请将此删除，默认用post方式提交
		before: function (obj) {
			
		},
		done: function (res, index, upload) {
			console.log(res.data.src)
			
			$('#userFace').attr('src', res.data.src);
			
		}
	});


	//添加验证规则
	form.verify({
		userBirthday: function (value) {
			if (!
				/^(\d{4})[\u4e00-\u9fa5]|[-\/](\d{1}|0\d{1}|1[0-2])([\u4e00-\u9fa5]|[-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/.test(
					value)) {
				return "出生日期格式不正确！";
			}
		}
	})
	
	//选择出生日期
	var userBirthday = localStorage.getItem("userBirthday"); 
	laydate.render({
		elem: '.userBirthday',
		format: 'yyyy年MM月dd日',
		trigger: 'click',
		value: userBirthday,
		max: 0,
		mark: {
			"0-12-15": "生日"
		},
		done: function (value, date) {
			// if(date.month === 12 && date.date === 15){ //点击每年12月15日，弹出提示语
			//     layer.msg('今天是马哥的生日，也是layuicms2.0的发布日，快来送上祝福吧！');
			// }
		}
	});

	//获取省信息
	
	//提交个人资料
	form.on("submit(changeUser)", function (data) {
		var index = layer.msg('提交中，请稍候', {
			icon: 16,
			time: false,
			shade: 0.8
		});
		LoginName = localStorage.getItem("LoginName");
		realname = $(".realName").val()
		sex = $('input[name="sex"]:checked').val()
		phone = $(".userPhone").val()
		userBirthday = $(".userBirthday").val()
		email = $(".userEmail").val()
		userImg = $("#userFace").attr("src")
		$.ajax({
			url: "http://127.0.0.1:8000/userChange",
			type: "post",
			dataType: "JSON",
			data: { "username": LoginName,"phone": phone,  "realname": realname, "sex": sex, "userBirthday": userBirthday, "email": email,'userImg':userImg },
			success: function (res) {
				console.log(res);
				if (res.code == 0) {
					
					layer.msg("提交成功！");
					window.location.href = '../../login2/login.html';
					// setTimeout(function () { }, 1000);
				}
				console.log('成功')

			}
		})
		//将填写的用户信息存到session以便下次调取
		return false;
		 //阻止表单跳转。如果需要表单跳转，去掉这段即可。
	});



	//判断是否设置过头像，如果设置过则修改顶部、左侧和个人资料中的头像，否则使用默认头像
	if (localStorage.getItem('userAvatar') && $(".userAvatar").length > 0) {
		var userAvatar = localStorage.getItem("userAvatar")
		$("#userFace").attr("src", userAvatar);
		$(".userAvatar").attr("src", userAvatar);
	} else {
		$("#userFace").attr("src", "../../../../images/face.jpg");
	}

	//判断是否修改过用户信息，如果修改过则填充修改后的信息
})
